package com.hpin.fansea.sale;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.GoodsCommentMy;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

/**
 * 商品评论页面
 * Created by Aaron on 15/10/22.
 */
public class GoodsMyCommentActivity extends BaseActivity {

    private EditText et_complaint;
    private String groupId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_my_comment);

        groupId = getIntent().getStringExtra("groupId");

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.comment));

        iv_left.setOnClickListener(this);

        et_complaint = (EditText) findViewById(R.id.et_complaint);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;

            case R.id.btn_commit: //提交

                if (TextUtils.isEmpty(et_complaint.getText().toString().trim())) {

                    showToast("请输入评论内容");
                    return;
                }

                comment();

                break;


        }
    }

    /**
     * 评论
     */
    private void comment() {

        RequestParams params = new RequestParams();
        params.addBodyParameter("token", sp.getString(Constant.token, ""));
        params.addBodyParameter("systemType", Constant.SYSTEM_TYPE);

        GoodsCommentMy commentMy = new GoodsCommentMy();

        commentMy.content = et_complaint.getText().toString().trim();
        commentMy.groupId = groupId;

        params.addBodyParameter("review", JSONObject.toJSONString(commentMy));

        HttpUtil.getInstance().setPostRequest("评论中...", this, Constant.WEB_ROOT + Constant.GOODSMYCOMMENT, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean baseBean = JSONObject.parseObject(str, ResultBaseBean.class);
                    if (Constant.STATUS_SUCESS.equals(baseBean.status)) {

                        showToast("发表成功");
                        finish();

                    }else {
                        showToast(baseBean.message);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }


            }
        }, null);

    }
}
